package com.sgb.kjwl.view.ui.fragment.transport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLogisticsMenuBinding;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.transport.GoodsCarsActivity;
import com.sgb.kjwl.view.widget.transport.menu.LogisticsMenuView;
import com.sgb.kjwl.viewmodel.self.carMakers.LogisticsMenuFragModel;
import com.sgb.link.entity.MenuEntity;
import com.swgk.core.dialog.DialogHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 黄淋
 * @date: 2021/9/7 16:42
 * @Description: 物流首页菜单
 */
public class LogisticsMenuFragment extends AppLazyFragment {
    LogisticsMenuFragModel viewModel;
    FragmentLogisticsMenuBinding binding;
    int page;

    public static LogisticsMenuFragment newInstance(int page) {
        LogisticsMenuFragment fragment = new LogisticsMenuFragment();
        Bundle args = new Bundle();
        args.putInt(RouterKey.TYPE, page);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_logistics_menu, container, false);
        viewModel = new ViewModelProvider(this).get(LogisticsMenuFragModel.class);
        binding.setLifecycleOwner(this);
        page = getArguments().getInt(RouterKey.TYPE, 0);
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }


    @Override
    protected void onLazyLoad() {
        switch (page) {
            case 0:
            case 1:
                viewModel.getGoodsType();
                break;
            case 2:
            case 3:
                viewModel.getCarTypeList();
                break;
        }
        viewModel.ldGoodsType.observe(this, this::setMenuData);
        viewModel.ldCarType.observe(this, this::setMenuData);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(String event) {
        if (event != null && event.equals(EventConstant.EVENT_REFRESH_MAIN_GOODS_TYPES)) {
            viewModel.getGoodsType();
        }
    }

    /**
     * 布局里添加数据
     *
     * @param list
     */
    private void setMenuData(List<GoodsInfoTypeEntity3> list) {
        List<MenuEntity> listdata = new ArrayList<>();
        MenuEntity menuEntity;
        for (GoodsInfoTypeEntity3 entity3 : list) {
            menuEntity = new MenuEntity();
            menuEntity.setName(entity3.getName());
            menuEntity.setId(entity3.getNo());
            menuEntity.setAppIcon(entity3.getKingKongDiagram());
            listdata.add(menuEntity);
        }

        binding.logisticsMenuView.initData(getChildFragmentManager(), listdata, new LogisticsMenuView.OnItemClick() {
            @Override
            public void onItemClick(MenuEntity entity, int position, int num) {
                DialogHelper.showProgressDialog(getContext(), "请求中...");
                switch (page) {
                    case 0://货运大厅
                        GoodsCarsActivity.start(getContext(), 0, false, entity.getId());
                        break;
                    case 1://竞价货运
                        GoodsCarsActivity.start(getContext(), 2, false, entity.getId());
                        break;
                    case 2://货车大厅
                        GoodsCarsActivity.start(getContext(), 1, true, entity.getId());
                        break;
                    case 3://竞价承运
                        GoodsCarsActivity.start(getContext(), 3, true, entity.getId());
                        break;
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
